<?php

/**
 * Anexo actions.
 *
 * @package    fiei
 * @subpackage Anexo
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class AnexoActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeEdit(sfWebRequest $request)
  {
  	$id        = $request->getParameter('id');
  	$nombre    = $request->getParameter('nombre');
  	$direccion = $request->getParameter('direccion');
    Deb::print_r($request->getParameterHolder()->getAll());
    
  }
  
  public function executeList(sfWebRequest $request)
  {
   ini_set('max_execution_time', 600); 
   $json   = new Services_JSON();
   $examp  = $request->getPostParameter('q'); 
   $page   = $request->getPostParameter('page'); 
   $limit  = $request->getPostParameter('rows'); 
   $sidx   = $request->getPostParameter('sidx'); 
   $sord   = $request->getPostParameter('sord'); 
   if($request->getParameter('codigo'))
   $codigo = $request->getParameter('codigo');
   else
   $codigo="";
   if($request->getParameter('nombre')) 
   $nombre   = $request->getParameter('nombre');
   else
   $nombre=""; 
   if(!$sidx) 
     $sidx = 1; 

   $q  = Doctrine_Query::create()->select('*')->from('Anexo a');

   if($codigo!='') 
     $q->addWhere(sprintf('LOWER(a.id) LIKE "%%%s%%"', $codigo)); 
   if($nombre!='')
     $q->addWhere(sprintf('LOWER(a.nombre) LIKE "%%%s%%"', $nombre));

   
   $count = $q->execute()->count();
    
   if( $count >0 ) 
     { $total_pages = ceil($count/$limit); } 
   else { $total_pages = 0; } 
   
   if ($page > $total_pages) 
     $page=$total_pages; 
   
   if ($limit<0) 
     $limit = 0; 
   $start = $limit*$page - $limit; 

   if ($start<0) 
     $start = 0; 
   
   if($sidx == 'distrito')
    $sidx = 'distrito_id';
   $q->orderBy($sidx.' '.$sord)
     ->limit($start.','.$limit);
    
   $results           = $q->execute();
   
   $response['page']    = $page; 
   $response['total']   = $total_pages; 
   $response['records'] = $count; 
   $i                   = 0; 
   
   foreach($results as $key => $result) 
   { 
     $response['rows'][$key]['id']   =  $result->getId(); 
     $response['rows'][$key]['cell'] = array($result->getId(),$result->getNombre(),$result->getDireccion(),$result->getDistrito()->getNombre());
   } 
   return $this->renderText($json->encode($response));
   //return sfView::NONE;
  }
}
